
// 渲染数据
$(function () {
    myRepaymentList();
});

function repayment(){
    var b_id = $('#b_id').val();
    var period_no = $('#period_no').val();
    
    var datas = {
        interface:'repayment@payment',  
        period_no:period_no,
        b_id:b_id
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                if(data.result.isJump){
                    //易宝的主动支付。切换连连报错
                    //location.href = data.result.content;
                    //连连的主动支付。
                    document.write(data.result.content);
                }else{
                    falert('缴费成功', '/order/mypayment?v='+Math.random());
                }
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

function myRepaymentList(){
    var datas = {
        interface:'repayment@myRepaymentList',     
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                if(data.result.countList.length>0){
                    $('#countList').html(common.formatTemplate(data.result.countList,$('#countList').html()));
                }else{
                    location.href = '/order/orderlist';
                }
                if(data.result.infoList.length>0){
                    $('#mypayment').show();
                    $('#infoList').html(common.formatTemplate(data.result.infoList,$('#infoList').html()));
                }else{
                    location.href = '/order/orderlist';
                }

                paymentSwiper(data.result.showId);
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

function paymentSwiper(showId){
    var Swiper1 = new Swiper('#swiper-container1', {
        //添加需要的选项:      
        centeredSlides:true, 
        slidesPerView :1.5,
        slidesOffsetBefore : 25,
        slidesOffsetAfter : 25,
        spaceBetween : 25,
        initialSlide :showId,
        onInit: function(swiper){
            var status_name = $('#countList .swiper-slide').eq(showId).find('.payment span').text();
            if(status_name != '立即缴费'){
                $('#countList .swiper-slide').eq(showId).find('.payment span').removeAttr("onclick");
                $('.ljhk').removeAttr("onclick").removeClass('paycolor').addClass('nopaycolor');
            }else{
                $('#countList .swiper-slide').eq(showId).find('.payment span').attr("onclick","repayment()");
                $('.ljhk').attr("onclick","repayment()").removeClass('nopaycolor').addClass('paycolor');
            }
            var total_amt = $('#countList .swiper-slide').eq(showId).find('.sum_amt').text();
            var b_id = $('#infoList .swiper-slide').eq(showId).find('t').text();
            $('#b_id').val(b_id);
            $('#total_amt').text(parseFloat(total_amt));
        },
        onSlideChangeEnd: function(swiper){
            var status_name = $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').text();
            if(status_name != '立即缴费'){
                $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').removeAttr("onclick");
                $('.ljhk').removeAttr("onclick").removeClass('paycolor').addClass('nopaycolor');
            }else{
                $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').attr("onclick","repayment()");
                $('.ljhk').attr("onclick","repayment()").removeClass('nopaycolor').addClass('paycolor');
            }
            var total_amt = $('#countList .swiper-slide').eq(swiper.activeIndex).find('.sum_amt').text();
            var b_id = $('#infoList .swiper-slide').eq(swiper.activeIndex).find('t').text();
            $('#total_amt').text(parseFloat(total_amt));
            $('#b_id').val(b_id);
            $('#period_no').val(swiper.activeIndex+1);
        }
    });
    var Swiper2 = new Swiper('#swiper-container2', {
        initialSlide :showId,
        onSlideChangeEnd: function(swiper){
            var status_name = $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').text();
            if(status_name != '立即缴费'){
                $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').removeAttr("onclick");
                $('.ljhk').removeAttr("onclick").removeClass('paycolor').addClass('nopaycolor');
            }else{
                $('#countList .swiper-slide').eq(swiper.activeIndex).find('.payment span').attr("onclick","repayment()");
                $('.ljhk').attr("onclick","repayment()").removeClass('nopaycolor').addClass('paycolor');
            }
            var total_amt = $('#countList .swiper-slide').eq(swiper.activeIndex).find('.sum_amt').text();
            var b_id = $('#infoList .swiper-slide').eq(swiper.activeIndex).find('t').text();
            $('#total_amt').text(parseFloat(total_amt));
            $('#b_id').val(b_id);
            $('#period_no').val(swiper.activeIndex+1);
        }
    });
    Swiper1.params.control = Swiper2;//需要在Swiper2初始化后，Swiper1控制Swiper2
    Swiper2.params.control = Swiper1;//需要在Swiper1初始化后，Swiper2控制Swiper1
}

function orderInfo(type) {
    var b_id = $('#b_id').val();
    if(type == '1'){
        location.href = '/order/jorderinfo?id='+b_id;
    }else{
        location.href = '/order/orderinfo?id='+b_id;
    }
}
